﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.KJBG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class EditPM : System.Web.UI.Page
{
    /// <summary>
    /// 科技报告操作对象
    /// </summary>
    private KJBG_DAO kjbgDAO = new KJBG_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {

        String message = null;
        //获取分类编码
        String wjm = Request["wjm"];

        KJBG_PM kjbgPM = null;
        KJBG_FL kjbgFL = null;


        if (null == wjm || "".Equals(wjm))
        {
            message = "错误：文件名为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            kjbgPM = this.kjbgDAO.GetKJBG_PMByWjm(wjm);
            if (null == kjbgPM)
            {
                message = "错误：没有找到对应的文件名";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
            kjbgFL = kjbgDAO.GetKJBG_FLByFlbm(kjbgPM.flbm);
            if (null == kjbgFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalKJBG.LISTPM_PAGE + "?catalog=" + kjbgFL.flbm;


        //分类编码
        this.flbmTextBox.Text = kjbgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = kjbgFL.flmc;
        //索引编码
        this.sybmTextBox.Text = kjbgPM.sybm;
        //文件名
        this.wjmHyperLink.NavigateUrl = GlobalResource.VIEW_PAGE + "?fileName=" + wjm;
        this.wjmHyperLink.Text = wjm;
        
        //报告编号
        this.bgbhTextBox.Text = kjbgPM.bgbh;
        //年份
        this.nfTextBox.Text = kjbgPM.nf.ToString();
        //报告名称
        this.bgmcTextBox.Text = kjbgPM.bgmc;
        //报告单位
        this.bgdwTextBox.Text = kjbgPM.bgdw;

        //作者
        this.zzTextBox.Text = kjbgPM.zz;

        //中文文摘
        this.zwwzTextBox.Text = kjbgPM.zwwz;
        //中文主题词
        this.zwztcTextBox.Text = kjbgPM.zwztc;
        //资料来源
        this.zllyTextBox.Text = kjbgPM.zlly;
        //开放
        this.kfCheckBox.Checked = kjbgPM.kf;
        //更新时间
        this.gxrqTextBox.Text = kjbgPM.gxrq.ToString();
        //页数
        this.ysTextBox.Text = kjbgPM.ys.ToString();
        //登录人
        this.dlrTextBox.Text = kjbgPM.dlr;

        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的科技报告篇名信息
    /// </summary>
    /// <returns></returns>
    private KJBG_PM GetKJBGPM()
    {
        KJBG_PM kjbgPM = new KJBG_PM();
        //索引编码
        kjbgPM.sybm = this.sybmTextBox.Text;
        //分类编码
        kjbgPM.flbm = this.flbmTextBox.Text;
        //文件名
        kjbgPM.wjm = this.wjmHyperLink.Text;
        //报告编号
        kjbgPM.bgbh = this.bgbhTextBox.Text.Trim();
        //年份
        kjbgPM.nf = Int32.Parse(this.nfTextBox.Text);
        //报告名称
        kjbgPM.bgmc = this.bgmcTextBox.Text.Trim();
        //报告单位
        kjbgPM.bgdw = this.bgdwTextBox.Text.Trim();
        //作者
        kjbgPM.zz = this.zzTextBox.Text.Trim();
        //中文文摘
        kjbgPM.zwwz = this.zwwzTextBox.Text.Trim();
        //中文主题词
        kjbgPM.zwztc = this.zwztcTextBox.Text.Trim();
        //页数
        try
        {
            kjbgPM.ys = Int32.Parse(this.ysTextBox.Text);
        }
        catch (Exception)
        {
        }

        //登录人
        kjbgPM.dlr = this.dlrTextBox.Text;

        //更新日期
        kjbgPM.gxrq = DateTime.Now;

        //资料来源
        kjbgPM.zlly = this.zllyTextBox.Text;
        //开放
        kjbgPM.kf = this.kfCheckBox.Checked;
        return kjbgPM;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateBGMC())
        {
            return;
        }
        if (!this.ValidateWJM())
        {
            return;
        }

        if (!ValidateYWJMFileUpload())
        {
            return;
        }


        if (null != this.ywjmFileUpload && null != this.ywjmFileUpload.FileName && !"".Equals(this.ywjmFileUpload.FileName))
        {
            if (!OperationFile.Upload(this.ywjmFileUpload, this.wjmHyperLink.Text))
            {
                Response.Write("<Script>alert('修改考察报告篇名信息失败')</Script>");
                return;
            }
        }

        KJBG_PM kjbgPM = this.GetKJBGPM();
        if (this.kjbgDAO.UpdateKJBG_PM(kjbgPM, ((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('修改科技报告篇名信息成功')</Script>");
            //this.BindData();
            this.NextPM();
        }
        else
        {
            Response.Write("<Script>alert('修改科技报告篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;
        return flag;
    }

    /// <summary>
    /// 验证报告名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateBGMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if (null == this.bgmcTextBox.Text || "".Equals(this.bgmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：报告名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            //this.errorLabel.Text = "错误：请选择上传的文件";
            flag = true;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysTextBox.Text = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }

    protected void bgdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.bgdwTextBox.Text = GlobalMethod.Semicolon(this.bgdwTextBox.Text);
    }
    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text);
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text);
    }
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }


    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalKJBG.EDITPM_PAGE + "?wjm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalKJBG.EDITPM_PAGE + "?wjm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String wjm = this.GetDirectionPM(1);
        if (null == wjm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalKJBG.EDITPM_PAGE + "?wjm=" + wjm);
        }
    }

    /// <summary>
    /// 获取下一个文件名，不包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取文件名的后三位流水号，并在其基础上加1
        int endIndex = this.wjmHyperLink.Text.LastIndexOf(".");

        String phString = this.wjmHyperLink.Text.Substring(endIndex - 3, 3);

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "0" + phString;
        }

        result = this.sybmTextBox.Text + "-" + phString + ".PDF";
        KJBG_PM kjbgPM = this.kjbgDAO.GetKJBG_PMByWjm(result);
        if (null == kjbgPM)
        {
            return null;
        }
        return result;
    }
}